<?php
/**
 ***********************************************************************************************************************
 * 远程DB调用(隔离进程)
 ***********************************************************************************************************************
 */
namespace soa\common;
use artisan\cache;
use soa\client;

/**
 * Class RemoteDBInvoke
 */
class RemoteDB extends client
{
    private $directConnect=true;//是否直连
    /**
     * @var Remote
     */
    protected $remote=null;
    protected $db='';
    protected $table='';
    protected $select='*';
    private $_joinTbls;
    private $_joinOns;
    private $_joinTypes;

    function __construct($db,$table,$remote='core')
    {
        $this->remote=new Remote($remote);
        $this->db=$db;
        $this->table=$table;
    }

    //重置条件
    private function reset()
    {
        $this->select = null;
        $this->_joinOns = null;
        $this->_joinTbls = array();
        $this->_joinTypes = null;
    }
    /**获取client实例，用于捕获http异常处理
     * @return Remote
     */
    public function getRemoteInstance(){
        return $this->remote;
    }

    /**
     * 表
     * @param string $table
     * @return $this
     */
    public function table($table){
        $this->table=$table;
        return $this;
    }


    /**查询字段
     * @param string $select
     * @return $this
     */
    public function select($select){
        $this->select=$select;
        return $this;
    }

    /**
     * @param string $joinTbl
     * @param string $joinOn
     * @param string $joinType
     * @return $this
     */
    public function join($joinTbl = '', $joinOn = '', $joinType = 'LEFT JOIN'){
        if(empty($joinTbl)) {
            return $this;
        }
        $this->_joinTbls= $joinTbl;
        $this->_joinTypes = $joinType;
        $this->_joinOns = $joinOn;
        return $this;
    }


    /**
     * 按条件检索一条数据
     * @param array $where
     * @param string $orderBy
     * @param bool $explain 是否分析查询语句
     * @return array|mixed
     */
    public function getOne($where = array(), $orderBy = '')
    {
        if($this->directConnect){
            return db::connect($this->db)->table($this->table)->getOne($where,$orderBy);
        }

        $res = $this->remote->post('/v1/RemoteDBInfc/invoke',[
            'db'=>$this->db,
            'table'=>$this->table,
            'select'=>$this->select,
            'joinTbls'=>$this->_joinTbls,
            'joinTypes'=>$this->_joinTypes,
            'joinOns'=>$this->_joinOns,
            'op'=>'getOne',
            'where'=>$where,
            'orderBy'=>$orderBy,
        ]);
        $this->reset();
        return $res;
    }

    /**
     * 获取多条数据
     *
     * @param array $where
     * @param string $limit
     * @param string $orderBy
     * @param bool $explain
     * @return array|bool|int|mixed
     */
    public function getAll($where = [], $limit = '', $orderBy = '')
    {
        if($this->directConnect){
            return db::connect($this->db)->table($this->table)->getAll($where,$limit,$orderBy);
        }

        cache::connect()->hset('shopinfo','uid:id',[]);


        $res = $this->remote->post('/v1/RemoteDBInfc/invoke',[
            'db'=>$this->db,
            'table'=>$this->table,
            'select'=>$this->select,
            'joinTbls'=>$this->_joinTbls,
            'joinTypes'=>$this->_joinTypes,
            'joinOns'=>$this->_joinOns,
            'op'=>'getAll',
            'where'=>$where,
            'limit'=>$limit,
            'orderBy'=>$orderBy,
        ]);
        $this->reset();
        return $res;
    }

    /**
     * @param array $where
     * @param $updateData
     * @return array|bool|int|mixed
     */
    public function update($where = array(), $updateData)
    {
        if($this->directConnect){
            return db::connect($this->db)->table($this->table)->update($where,$updateData);
        }

        $res = $this->remote->post('/v1/RemoteDBInfc/invoke',[
            'db'=>$this->db,
            'table'=>$this->table,
            'op'=>'update',
            'where'=>$where,
            'updateData'=>$updateData,
        ]);
        $this->reset();
        return $res;
    }

    /**
     * 插入
     * @param $insertData
     * @return bool
     */
    public function insert($insertData)
    {
        if($this->directConnect){
            return db::connect($this->db)->table($this->table)->insert($insertData);
        }

       $res = $this->remote->post('/v1/RemoteDBInfc/invoke',[
            'db'=>$this->db,
            'table'=>$this->table,
            'op'=>'insert',
            'insertData'=>$insertData,
        ]);
        $this->reset();
        return  $res;
    }

    /**
     * 删除
     * @param array $where
     * @return bool
     */
    public function delete($where = array())
    {
        if($this->directConnect){
            return db::connect($this->db)->table($this->table)->delete($where);
        }

        $res = $this->remote->post('/v1/RemoteDBInfc/invoke',[
            'db'=>$this->db,
            'table'=>$this->table,
            'op'=>'delete',
            'where'=>$where,
        ]);
        $this->reset();
        return $res;
    }

    /**
     * 直接操作sql
     * @param $sql
     * @return array|bool|int|mixed
     */
    public function query($sql)
    {
        if($this->directConnect){
            return db::connect($this->db)->table($this->table)->query($sql);
        }

        return $this->remote->post('/v1/RemoteDBInfc/invoke',[
            'db'=>$this->db,
            'table'=>$this->table,
            'op'=>'query',
            'sql'=>$sql,
        ]);
    }

    /**
     * 获取上次插入id
     */
    public function getInsertId()
    {
        if($this->directConnect){
            return db::connect($this->db)->table($this->table)->getInsertId();
        }

        return $this->remote->post('/v1/RemoteDBInfc/invoke',[
            'db'=>$this->db,
            'table'=>$this->table,
            'op'=>'getInsertId'
        ]);
    }


    /**
     * 获取操作影响行数
     */
    public function getAffectedRows()
    {
        if($this->directConnect){
            return db::connect($this->db)->table($this->table)->getAffectedRows();
        }

        return $this->remote->post('/v1/RemoteDBInfc/invoke',[
            'db'=>$this->db,
            'table'=>$this->table,
            'op'=>'getAffectedRows'
        ]);
    }

    /**
     * 上一条查询语句
     * @return string
     */
    public function getLastQuery()
    {
        if($this->directConnect){
            return db::connect($this->db)->table($this->table)->getLastQuery();
        }

        return $this->remote->post('/v1/RemoteDBInfc/invoke',[
            'db'=>$this->db,
            'table'=>$this->table,
            'op'=>'getLastQuery'
        ]);
    }

    /**
     * 按条件检索统计数据
     * @param array $where
     * @return int|mixed
     */
    public function count($where=[])
    {
        if($this->directConnect){
            return db::connect($this->db)->table($this->table)->count($where);
        }

        return $this->remote->post('/v1/RemoteDBInfc/invoke',[
            'db'=>$this->db,
            'table'=>$this->table,
            'op'=>'count',
            'where'=>$where
        ]);
    }


}
